package com.test.demo;

import android.Manifest;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.hikvision.netsdk.ExceptionCallBack;
import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.NET_DVR_DEVICEINFO_V30;
import com.hikvision.netsdk.NET_DVR_PREVIEWINFO;
import com.hikvision.netsdk.RealPlayCallBack;

import org.MediaPlayer.PlayM4.Player;
import org.MediaPlayer.PlayM4.PlayerCallBack;

import static com.test.demo.ScreenTest.TAG;

public class LiveActivity extends Activity implements SurfaceHolder.Callback {


    private NET_DVR_DEVICEINFO_V30 m_oNetDvrDeviceInfoV30 = null;

    private static boolean m_iInitSdk = false; //初始化sdk结果
    public static int m_iLogID = -1; // return by NET_DVR_Login_v30
    private int m_iPlayID = -1; // return by NET_DVR_RealPlay_V30
    private int m_iPlaybackID = -1; // return by NET_DVR_PlayBackByTime

    private static int m_iPort = -1; // play port
    public static int m_iStartChan = 0; // start channel no
    private int m_iChanNum = 0; // channel number
    //private static PlaySurfaceView[] playView = new PlaySurfaceView[4];
    private SurfaceView m_osurfaceView = null;

    private boolean m_bMultiPlay = false;

    private boolean m_bNeedDecode = true;
    private boolean m_bSaveRealData = false;
    private boolean m_bStopPlayback = false;

    //
    private CameraInfo camera;
    private boolean isGetCamera = false;
    private boolean mGetVideo = false;//是否获取视频

    private ImageView btn_play_stop;


    int buffsize = 10;
    byte[][] TappData = new byte[buffsize][];
    boolean bFirst = true;
    int[] nHaveW = new int[buffsize];
    int nWrite = 0;
    int nRead = 0;
    // byte[] TappData2 = null;
    int TnWidth, TnHeight;

    int Tfirst = 0;

    public class CameraInfo {
        public int id;
        public String address;
        public int port;
        public String account;
        public String password;
        public String name;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
//                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 不锁屏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
        setContentView(R.layout.activity_live);

        String[] permName = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        boolean bCheck = PermissionUtils.verifyPermissions(this, permName);
        if (bCheck) {
            Log.d(getClass().getName(), "get storage permission ");
        }

        btn_play_stop = findViewById(R.id.btn_play_stop);
        btn_play_stop.setOnClickListener(new clickListener());

        m_osurfaceView = findViewById(R.id.Sur_Player);//surfaceview无法调用oncreate原因是surfaceView不可见或者没有挂到事件上。
        m_osurfaceView.getHolder().addCallback(this);
        // 竖屏
        //    m_osurfaceView.setLayoutParams(new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,640*application.height/AppConstants.DESIGN_HEIGHT,Gravity.CENTER));
        // 横屏
        //m_osurfaceView.setLayoutParams(new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT,Gravity.CENTER));
        //m_osurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //m_osurfaceView.setZOrderOnTop(true);

//        DisplayMetrics metric = new DisplayMetrics();
//        getWindowManager().getDefaultDisplay().getMetrics(metric);
//
//        for (int i = 0; i < 4; i++) {
//            if (playView[i] == null) {
//                playView[i] = new PlaySurfaceView(this);
//                playView[i].setParam(metric.widthPixels);
//                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
//                        FrameLayout.LayoutParams.WRAP_CONTENT,
//                        FrameLayout.LayoutParams.WRAP_CONTENT);
//                params.bottomMargin = playView[i].getM_iHeight() - (i / 2)
//                        * playView[i].getM_iHeight();
//                params.leftMargin = (i % 2) * playView[i].getM_iWidth();
//                params.gravity = Gravity.BOTTOM | Gravity.LEFT;
//                addContentView(playView[i], params);
//                playView[i].setVisibility(View.INVISIBLE);
//            }
//        }
//        playView[0].setVisibility(View.VISIBLE);

        setCameraInfo();

        initial();

    }

    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            playSingle();
        }
    }

    private void initial() {
        try {
            if (!camera.account.equals("")) {
                m_iInitSdk = initeSdk();
                if (!m_iInitSdk) {
                    Log.e("", "LIVE----摄像头初始化异常退出");
                    Toast.makeText(LiveActivity.this, "初始化监控设备失败，请稍后再试", Toast.LENGTH_SHORT).show();
                } else {
                    login();
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    private void playSingle()
    {

//                    if (m_iChanNum > 1)// preview more than a channeoexceptionCbfl
//                    {
//                        if (!m_bMultiPlay) {
//
//                            stopSinglePlayer();
//
//                            startMultiPreview();
//                            m_bMultiPlay = true;
//                        } else {
//                            stopMultiPreview();
//                            m_bMultiPlay = false;
//                        }
//                    } else // preivew a channel
//                    {
//                        if (m_iPlayID < 0) {
//                            startSinglePreview();
//                        } else {
//                            stopSinglePreview();
//                        }
//                    }

        startSinglePreview();
    }

    private void setCameraInfo() {
        camera = new CameraInfo();
        camera.id = 100;
        camera.address = "192.168.0.100";
        camera.port = 8000;
        camera.account = "admin";
        camera.password = "ss123456";
        camera.name = "admin";
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        Player.getInstance().stop(m_iPort);
        // release player resource

        Player.getInstance().freePort(m_iPort);
        m_iPort = -1;

        // release net SDK resource
        HCNetSDK.getInstance().NET_DVR_Cleanup();

        m_iLogID = -1; // return by NET_DVR_Login_v30
        m_iPort = -1; // play port
        m_iStartChan = 0; // start channel no
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.e("", "屏幕设置为：横屏");
//            m_osurfaceView.setLayoutParams(new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));
//			m_osurfaceView.setLayoutParams(new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,360*application.height/application.DESIGN_HEIGHT,Gravity.CENTER));

        } else {
            Log.e("", "屏幕设置为：竖屏");
            // m_osurfaceView.setLayoutParams(new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,640*application.height/AppConstants.DESIGN_HEIGHT,Gravity.CENTER));
        }
//        String message=newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE ? "屏幕设置为：横屏" : "屏幕设置为：竖屏";
//
//        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i(TAG, "surface is created" + m_iPort);
        holder.setFormat(PixelFormat.TRANSLUCENT);// //m_osurfaceView.getHolder()

        m_iPort = Player.getInstance().getPort();//一定要保障surfaceview被显示，不能被遮挡。
        if (-1 == m_iPort) {
            return;
        }

        //SurfaceHolder holder = m_osurfaceView.getHolder();
        Surface surface = holder.getSurface();
        if (surface.isValid()) {
            if (!Player.getInstance().setVideoWindow(m_iPort, 0, holder)) {
                Log.e(TAG, "Player setVideoWindow failed!");
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub
        holder.setFormat(PixelFormat.TRANSLUCENT);
        System.out.println("surfaceChanged");
    }

    // @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        System.out.println("surfaceDestroyed");

        // new add
        if (-1 == m_iPort) {
            return;
        }
        Surface surface = holder.getSurface();
        if (surface.isValid()) {
            if (!Player.getInstance().setVideoWindow(m_iPort, 0, null)) {
                Log.e(TAG, "Player setVideoWindow failed!");
            }
        }
    }

//    private void startMultiPreview() {
//        DisplayMetrics metric = new DisplayMetrics();
//        getWindowManager().getDefaultDisplay().getMetrics(metric);
//        int i = 0;
//        for (i = 0; i < 4; i++) {
//            if (playView[i] == null) {
//                playView[i] = new PlaySurfaceView(this);
//                playView[i].setParam(metric.widthPixels);
//                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
//                        FrameLayout.LayoutParams.WRAP_CONTENT,
//                        FrameLayout.LayoutParams.WRAP_CONTENT);
//                params.bottomMargin = playView[i].getM_iHeight() - (i / 2)
//                        * playView[i].getM_iHeight();
//                params.leftMargin = (i % 2) * playView[i].getM_iWidth();
//                params.gravity = Gravity.BOTTOM | Gravity.LEFT;
//                addContentView(playView[i], params);
//            }
//            playView[i].startPreview(m_iLogID, m_iStartChan + i);//创建surfaceview
//        }
//        m_iPlayID = playView[0].m_iPreviewHandle;
//    }

//    private void stopMultiPreview() {
//        int i = 0;
//        for (i = 0; i < 4; i++) {
//            playView[i].stopPreview();
//        }
//        m_iPlayID = -1;
//    }

    private void startSinglePreview() {
        RealPlayCallBack fRealDataCallBack = getRealPlayerCbf();

        NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO();
        previewInfo.lChannel = m_iStartChan;
        previewInfo.dwStreamType = 1; // substream 0 表示主码流，为 1 表示子码流
        previewInfo.bBlocked = 1;// 0- 非阻塞取流，1- 阻塞取流
        // NET_DVR_CLIENTINFO struClienInfo = new NET_DVR_CLIENTINFO();
        // struClienInfo.lChannel = m_iStartChan;
        // struClienInfo.lLinkMode = 0;
        // HCNetSDK start preview

        //previewInfo.hHwnd = m_osurfaceView.getHolder();//todo 查看视频需打开

        m_iPlayID = HCNetSDK.getInstance().NET_DVR_RealPlay_V40(m_iLogID,//登录ipc
                previewInfo, fRealDataCallBack);
        // m_iPlayID = HCNetSDK.getInstance().NET_DVR_RealPlay_V30(m_iLogID,
        // struClienInfo, fRealDataCallBack, false);
        if (m_iPlayID < 0) {
            Log.e(TAG, "NET_DVR_RealPlay is failed!Err:"
                    + HCNetSDK.getInstance().NET_DVR_GetLastError());
            return;
        }
    }

    private void stopSinglePreview() {
        // net sdk stop preview
        if (!HCNetSDK.getInstance().NET_DVR_StopRealPlay(m_iPlayID)) {
            Log.e(TAG, "StopRealPlay is failed!Err:"
                    + HCNetSDK.getInstance().NET_DVR_GetLastError());
            return;
        }
        m_iPlayID = -1;
        stopSinglePlayer();
    }

    private void stopSinglePlayer() {
        Player.getInstance().stopSound();
        // player stop play
        if (!Player.getInstance().stop(m_iPort)) {
            Log.e(TAG, "stop is failed!");
            return;
        }

        if (!Player.getInstance().closeStream(m_iPort)) {
            Log.e(TAG, "closeStream is failed!");
            return;
        }
        if (!Player.getInstance().freePort(m_iPort)) {
            Log.e(TAG, "freePort is failed!" + m_iPort);
            return;
        }
        m_iPort = -1;
    }

    private RealPlayCallBack getRealPlayerCbf() {
        RealPlayCallBack cbf = new RealPlayCallBack() {
            public void fRealDataCallBack(int iRealHandle, int iDataType,
                                          byte[] pDataBuffer, int iDataSize) {
                // player channel 1
               //把数据填充到player中
               processRealData(1, iDataType, pDataBuffer, iDataSize, Player.STREAM_REALTIME);

            }
        };

        return cbf;
    }

    private void setDecodeCB() {
        try {

//				Player.getInstance().setDecodeFrameType(m_iPort, 0);
// 设置视频帧解码类型0 正常解码 1只解关键帧 2不解视频帧
            Player.getInstance().setDecodeCB(m_iPort, new PlayerCallBack.PlayerDecodeCB() {

                @Override
                public void onDecode(int arg0, final byte[] data,
                                     int nDataLen, int nWidth, int nHeight, int arg5,
                                     int arg6, int arg7) {
                    try {
                        // TODO Auto-generated method stub
//							Log.e("", "onDecode回调");
                        if (bFirst) {
                            //申请10个缓冲区，每个固定nDataLen大小。测试得到每次帧大小都是固定的
                            TnWidth = nWidth;
                            TnHeight = nHeight;
                            for (int i = 0; i < buffsize; i++) {
                                TappData[i] = new byte[nDataLen];
                                nHaveW[i] = 0;//标识当前缓冲没有写入
                            }
                            bFirst = false;
                        }


                        if (nHaveW[nWrite] == 0) {

                            {
//					Log.e("", "data len:" + data.length + "TappDataz值结果：" + TappData[nWrite]);
//					Log.e("", "索引位置" + nWrite + "数组写入索引" + nHaveW[nWrite]);
                                System.arraycopy(data, 0, TappData[nWrite], 0,
                                        data.length);
                                nHaveW[nWrite] = 1;//标识当前缓冲写入
                                nWrite++;
                                if (nWrite >= buffsize) {//循环使用nWrite
                                    nWrite = 0;
                                }
//					data=null;
                            }
                            byte[] i420bytes = null;
                            {
                                if (nHaveW[nRead] != 0) {
                                    i420bytes = swapYV12toI420(TappData[nRead], TnWidth, TnHeight);
//						Log.e("", "索引位置" + nRead + "数组读写索引" + nHaveW[nRead]);
                                    nHaveW[nRead] = 0;//读取后，标识当前缓冲没有写入
                                    nRead += 1;
                                    if (nRead >= buffsize)//循环使用nRead
                                        nRead = 0;
                                }
                            }
                            {
                                // process incoming messages here
                                if (i420bytes != null) {
                                    //选择两种分辨率 640, 480 /1280, 720

//todo								.fillExternalCaptureFrame(i420bytes,
////										i420bytes.length, 1280, 720, 0,
//										i420bytes.length, 640, 480, 0,
//										AVVideoCtrl.EXTERNAL_FORMAT_I420,
//										AVView.VIDEO_SRC_TYPE_CAMERA);
//使用回调函数处理
                                }
                            }
                        }

                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }

            });

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public void processRealData(int iPlayViewNo, int iDataType,
                                byte[] pDataBuffer, int iDataSize, int iStreamMode) {
        if (!m_bNeedDecode) {
            // Log.i(TAG, "iPlayViewNo:" + iPlayViewNo + ",iDataType:" +
            // iDataType + ",iDataSize:" + iDataSize);
        } else {
            //NET_DVR_SYSHEAD  1  系统头数据
            //NET_DVR_STREAMDATA  2  流数据（包括复合流或音视频分开的视频流数据）
            //NET_DVR_AUDIOSTREAMDATA  3  音频数据
            if (HCNetSDK.NET_DVR_SYSHEAD == iDataType) {
                if (m_iPort < 0) {
                    m_iPort = Player.getInstance().getPort();
                }
                if (m_iPort == -1) {
                    Log.e(TAG, "getPort is failed with: "
                            + Player.getInstance().getLastError(m_iPort));
                    return;
                }

                Log.i(TAG, "getPort succ with: " + m_iPort);
                if (iDataSize > 0) {
                    if (!Player.getInstance().setStreamOpenMode(m_iPort,
                            iStreamMode)) // set stream mode
                    {
                        Log.e(TAG, "setStreamOpenMode failed");
                        return;
                    }
                    if (!Player.getInstance().openStream(m_iPort, pDataBuffer,
                            iDataSize, 2 * 1024 * 1024)) // open stream
                    {
                        Log.e(TAG, "openStream failed");
                        return;
                    }
                    ////////////////////////////////
                    if (!Player.getInstance().setDisplayBuf(m_iPort, 6)) // 设置播放缓冲区最大缓冲帧数 6 表示自适应模式，
                    {
                        Log.e(TAG, "openStream failed");
                        return;
                    }
                    setDecodeCB();//设置解码回调
                    //setDisplayCB();//设置播放回调
                    /////////////////////////////// todo
//                    SurfaceHolder holder = m_osurfaceView.getHolder();
//                    Surface surface = holder.getSurface();
//                    if (surface.isValid()) {
//                        if (!Player.getInstance().setVideoWindow(m_iPort, 0, holder)) {
//                            Log.e(TAG, "Player setVideoWindow failed!");
//                        }
//                    }
                    SurfaceHolder holder = null;
                    if (!Player.getInstance().play(m_iPort, holder)) {
                        Log.e(TAG, "play failed");
                        return;
                    }
//					if (!Player.getInstance().playSound(m_iPort)) {
//						Log.e(TAG, "playSound failed with error code:"
//								+ Player.getInstance().getLastError(m_iPort));
//						return;
//					}
                }
            } else {
                boolean putDate = Player.getInstance().inputData(m_iPort, pDataBuffer, iDataSize);

                if (!putDate) {//播放失败
                    // Log.e(TAG, "inputData failed with: " +
                    // Player.getInstance().getLastError(m_iPort));
                    for (int i = 0; i < 4000 ; i++) {
                        if (Player.getInstance().inputData(m_iPort,
                                pDataBuffer, iDataSize)) {
                            break;

                        }
                        if (i % 100 == 0) {
                            Log.e(TAG, "inputData failed with: "
                                    + Player.getInstance()
                                    .getLastError(m_iPort) + ", i:" + i);
                        }
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();

                        }
                    }
                }

            }
        }

    }


    int num = 0;
    long timeTmp = 0;

    byte[] i420bytes = null;

    private byte[] swapYV12toI420(byte[] yv12bytes, int width, int height) {
        // byte[] i420bytes = null;

        // // long tTmpB = Calendar.getInstance().getTimeInMillis();
        // if (i420bytes == null)
        // i420bytes = new byte[yv12bytes.length];
        // if (yv12bytes.length != i420bytes.length) {
        // Log.e("", "----------------------------------");
        // return null;
        // }

        if (null == i420bytes) {
            i420bytes = new byte[yv12bytes.length];
        }

        try {

            //System.arraycopy(yv12bytes, 0,  i420bytes, 0, width * height);
//			for (int i = width * height; i < width * height+ (width / 2 * height / 2); i++)
//				i420bytes[i] = yv12bytes[i + (width / 2 * height / 2)];
//			for (int i = width * height + (width / 2 * height / 2); i < width* height + 2 * (width / 2 * height / 2); i++)
//				i420bytes[i] = yv12bytes[i - (width / 2 * height / 2)];
//			System.arraycopy(i420bytes, width * height, yv12bytes, width * height, width * height/2);
///////////
            int nTemp = width * height;
            System.arraycopy(yv12bytes, nTemp + (nTemp / 4), i420bytes, nTemp, nTemp / 4);
            System.arraycopy(yv12bytes, nTemp, i420bytes, nTemp + (nTemp / 4), nTemp / 4);
            System.arraycopy(i420bytes, nTemp, yv12bytes, nTemp, nTemp / 2);

//			System.arraycopy(i420bytes, 0, yv12bytes, 0, nTemp);
//			System.arraycopy(i420bytes,nTemp + nTemp/4, yv12bytes, nTemp, nTemp/4);
//			System.arraycopy(i420bytes, nTemp, yv12bytes, nTemp + nTemp/4 , nTemp/4);

            /////////////////////
        } catch (Exception e) {
            Log.e("", "----------------------------------" + e.getMessage());
            return null;
        }

        // if (num==100) {
        // Log.e("", "the all time = " + timeTmp + "one time is " +
        // timeTmp/100);
        // num=0;
        // }
        // num++;
        // timeTmp += Calendar.getInstance().getTimeInMillis() - tTmpB;

        return yv12bytes;
    }

    private int loginNormalDevice() {
        // get instance
        m_oNetDvrDeviceInfoV30 = new NET_DVR_DEVICEINFO_V30();
        if (null == m_oNetDvrDeviceInfoV30) {
            Log.e(TAG, "HKNetDvrDeviceInfoV30 new is failed!");
            return -1;
        }

        String strIP = camera.address;
        int nPort = camera.port;
        String strUser = camera.account;
        String strPsd = camera.password;
        // call NET_DVR_Login_v30 to login on, port 8000 as default
        int iLogID = HCNetSDK.getInstance().NET_DVR_Login_V30(strIP, nPort,
                strUser, strPsd, m_oNetDvrDeviceInfoV30);
        if (iLogID < 0) {
            Log.e(TAG, "NET_DVR_Login is failed!Err:"
                    + HCNetSDK.getInstance().NET_DVR_GetLastError());
            return -1;
        }
        if (m_oNetDvrDeviceInfoV30.byChanNum > 0) {
            m_iStartChan = m_oNetDvrDeviceInfoV30.byStartChan;
            m_iChanNum = m_oNetDvrDeviceInfoV30.byChanNum;
        } else if (m_oNetDvrDeviceInfoV30.byIPChanNum > 0) {
            m_iStartChan = m_oNetDvrDeviceInfoV30.byStartDChan;
            m_iChanNum = m_oNetDvrDeviceInfoV30.byIPChanNum
                    + m_oNetDvrDeviceInfoV30.byHighDChanNum * 256;
        }
        Log.i(TAG, "NET_DVR_Login is Successful!");

        return iLogID;
    }

    private boolean initeSdk() {
        // init net sdk
        if (!HCNetSDK.getInstance().NET_DVR_Init()) {
            Log.e(TAG, "HCNetSDK init is failed!");
            return false;
        }
        HCNetSDK.getInstance().NET_DVR_SetLogToFile(3, "/mnt/sdcard/sdklog/",
                true);
        return true;
    }

    // login listener
    private int login() {
        try {
            if (m_iLogID < 0) {
                // login on the device
                m_iLogID = loginNormalDevice();
                if (m_iLogID < 0) {
                    Log.e(TAG, "This device logins failed!");
                    return m_iLogID;
                } else {
                    System.out.println("m_iLogID=" + m_iLogID);
                }
                if (m_iLogID != -1) {
                    isGetCamera = true;
                    //startSinglePreview();
                } else {
                    isGetCamera = false;
                }
                // get instance of exception callback and set
                ExceptionCallBack oexceptionCbf = getExceptiongCbf();
                if (oexceptionCbf == null) {
                    Log.e(TAG, "ExceptionCallBack object is failed!");
                    return m_iLogID;
                }

                if (!HCNetSDK.getInstance().NET_DVR_SetExceptionCallBack(
                        oexceptionCbf)) {
                    Log.e(TAG, "NET_DVR_SetExceptionCallBack is failed!");
                    return m_iLogID;
                }
                Log.e(TAG,
                        "监控Login sucess ****************************1***************************");

            } else {
                // whether we have logout
                if (!HCNetSDK.getInstance().NET_DVR_Logout_V30(m_iLogID)) {
                    Log.e(TAG, " NET_DVR_Logout is failed!");
                    return m_iLogID;
                }
                m_iLogID = -1;
            }
        } catch (Exception err) {
            Log.e(TAG, "error: " + err.toString());
            return m_iLogID = -1;
        }
        return m_iLogID;
    }

    private ExceptionCallBack getExceptiongCbf() {
        ExceptionCallBack oExceptionCbf = new ExceptionCallBack() {
            public void fExceptionCallBack(int iType, int iUserID, int iHandle) {
                System.out.println("recv exception, type:" + iType);
            }
        };
        return oExceptionCbf;
    }

}
